<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // 加上遍历器方法之一
    // function* objectEntries(obj) {
    //   let propKeys = Reflect.ownKeys(obj);

    //   for(let propKey of propKeys) {
    //     yield [propKey, obj[propKey]];
    //   }
    // }

    // let jane = {first: 'Jane', last: 'Doe' };
    // for(let [key,value] of objectEntries(jane)) {
    //   console.log(`${key}: ${value}`);
    // }
    //由于对象jane原生不具备Iterator接口，无法用for...of遍历，这是通过Generator函数
    //的objectEntries为它加上遍历器接口，就可以用for...of遍历
    
    //加上遍历器方法之二
    function* objectEntries() {
      let propKeys = Object.keys(this);

      for(let propKey of propKeys) {
        yield [propKey, this[propKey]];
      }
    }

    let jane = {first: 'Jane', last: 'Doe' };
    
    jane[Symbol.iterator] = objectEntries;
    for(let [key,value] of jane) {
      console.log(`${key}: ${value}`);
    }
  </script>
</body>
</html>